﻿using CodeGenerator.DBInfo.entity;
using MySql.Data.MySqlClient;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator.DBInfo
{
    abstract class AbstractDbInfoGetter<T, D>: IDBInfoGetter
        where T : IDbConnection
        where D : IDataAdapter
    {
        public ConnectionInfo ConnInfo { get; }
        public AbstractDbInfoGetter(ConnectionInfo connInfo)
        {
            this.ConnInfo = connInfo;
        }
        /// <summary>
        /// 获取DataAdapter
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="conn">数据库连接</param>
        /// <returns>DataAdapter</returns>
        public abstract D GetDataAdapter(string sql, IDbConnection conn);
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns>返回数据库连接对象</returns>
        public abstract T Connect();
        /// <summary>
        /// 查询sql并返回DataTable
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>数据集</returns>
        public DataTable Query(string sql)
        {
            DataSet ds = new DataSet();
            var conn = Connect();
            conn.Open();
            var adapter = (IDataAdapter)GetDataAdapter(sql, conn);
            adapter.Fill(ds);
            conn.Close();
            return ds.Tables.Count > 0 ? ds.Tables[0] : null;
        }

        public abstract void TestConnection();

        public abstract List<string> GetDbList();
        public abstract List<string> GetSchemaList(string dbName);
        public abstract List<dynamic> GetTableList(string parentName);
        public abstract TableInfo GetTableInfo(string db, string schema, string table);
        public abstract string GetJavaType(ColumnInfo column);
        public abstract string GetJavaScriptType(ColumnInfo column);
    }
}
